package nero.yncc.com.createart.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterChoosePopChangjing;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanGoodChangjing;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.bean.UserBean;

/**
 * Created by nero on 17-7-5.
 */

public class PopGuigeChangjing implements View.OnClickListener{
    Context context;
    ArrayList<BeanGoodChangjing> arrayList_goods;

    Button btn_addShopCar;
    UserBean userBean;
    View view_root;
    PopupWindow popupWindow;
    AdapterChoosePopChangjing adapterChoosePopChangjing;
    String changjingID;
    ListView lv_list;
    public PopGuigeChangjing(Context context, ArrayList<BeanGoodChangjing> godBean, UserBean userBean,String changjingID) {
        this.context = context;
        this.userBean=userBean;
        this.changjingID=changjingID;

        view_root=LinearLayout.inflate(context,R.layout.pop_chooseguige_changjing,null);
        popupWindow = new PopupWindow(view_root, -1, -1);
        this.arrayList_goods = godBean;
        //吧0个的商品都变成1个
        for (BeanGoodChangjing goodsBean:arrayList_goods){
            if(goodsBean.getNum()==0){
                goodsBean.setNum(1);
            }
//            goodsBean.setChoose(false);
        }
        lv_list=view_root.findViewById(R.id.lv_list);
        btn_addShopCar=view_root.findViewById(R.id.btn_addShopCar);
        btn_addShopCar.setOnClickListener(this);
        adapterChoosePopChangjing=new AdapterChoosePopChangjing(context,arrayList_goods,this);
        lv_list.setAdapter(adapterChoosePopChangjing);
    }

    public void show(Activity activity) {
        try {
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        }catch (Exception e){}

    }

    public boolean isShow(){
        return popupWindow.isShowing();
    }

    public void disMiss() {
        popupWindow.dismiss();
    }

    Boolean isChooseAll = true;//是否所有规格都进行了选择
    String specStr = "";
    String price = "";
    String marketPrice = "";
    public void setGuigeMoney() {
        //便利规格的选择 串联起来
//        tv_please_choose.setText("");

//        {"goods_id": 101,"spec_list": [{"spec_id": 35,"item_id": 155}], "price": 100,"market_price": 250, "number": 1}
        specStr = "[";
        isChooseAll = true;

        for (BeanGoodChangjing beanGoodChangjing:arrayList_goods){
            if(beanGoodChangjing.isChoose()){
                specStr+="{\"goods_id\": " + beanGoodChangjing.getGoods().getGoods_id() + ",\"spec_list\": [";

                for (GodBean.SpecAttr specAttr : beanGoodChangjing.getGoods().getGoods_spec_list()) {
//            {"spec_id": 35,"item_id": 155}
                    specStr += "{\"spec_id\":"+specAttr.getSpec_id() + ",\"item_id\":" + specAttr.getChooseAttrId() + "},";
                }
                if(specStr.endsWith(",")){
                    specStr=specStr.substring(0,specStr.length()-1);
                }
                if(specStr.indexOf("spec_id")>0){
                    specStr+="],";
                }

                specStr +=" \"price\": "+beanGoodChangjing.getGoods().getShop_price()+",\"market_price\": "+beanGoodChangjing.getGoods().getMarket_price()+", \"number\": "+beanGoodChangjing.getNum()+"},";

            }
        }
        if(specStr.endsWith(",")){
            specStr=specStr.substring(0,specStr.length()-1);
        }
        specStr += "]";
        Log.e("123",specStr);

    }


    private void downOrder(){
        setGuigeMoney();
        if(TextUtils.isEmpty(specStr) || specStr.equals("[]")){
            ToastUtil.showToast(context,"请先选择商品");
            return;
        }
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("id",changjingID);
        hashMap.put("user_id",userBean.getUser_id());
        hashMap.put("content",specStr);

        new HttpUtils().post((Activity) context, Constants.API_ADD_CHANGJING_SHOPCAR, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }

            @Override
            public void fail(String failMsg) {
                Toast.makeText(context,failMsg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_addShopCar:
                downOrder();
                break;
        }
    }
}
